package com.clz.workorder.fragment

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.clz.workorder.BR
import com.clz.workorder.R
import com.clz.workorder.adapter.AttachImageAdapter
import com.clz.workorder.adapter.OrderStateAdapter
import com.clz.workorder.databinding.FragmentComplaintDetailBinding
import com.clz.workorder.view.FullyGridLayoutManager
import com.clz.workorder.viewmodel.ComplaintDetailViewModel
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.config.ComplaintType
import com.czl.base.config.OrderType
import com.czl.base.event.LiveBusCenter
import com.czl.base.util.DialogHelper

@Route(path = AppConstants.Router.Complaint.F_COMPLAINT_DETAIL)
class ComplaintDetailFragment: BaseFragment<FragmentComplaintDetailBinding, ComplaintDetailViewModel>() {
    lateinit var oAdapter: AttachImageAdapter
    lateinit var sAdapter: OrderStateAdapter
    override fun initContentView(): Int {
        return R.layout.fragment_complaint_detail
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }
    override fun initData() {
        viewModel.orderNo.set(arguments?.getString(AppConstants.BundleKey.WORK_ORDER_NO).toString())
//        viewModel.isMine.set(arguments?.getBoolean(AppConstants.BundleKey.WORK_ORDER_IS_MINE,false))
        viewModel.tvTitle.set("投诉详情")
        viewModel.getData()
        oAdapter=AttachImageAdapter(this).apply {

        }
        sAdapter= OrderStateAdapter(this)
    }
    @SuppressLint("NotifyDataSetChanged")
    override fun initViewObservable() {
        binding.rvReportSubmit.apply {
            layoutManager= FullyGridLayoutManager(context,4, GridLayoutManager.VERTICAL, false)
            adapter=oAdapter
        }
        viewModel.attachImagesEvent.observe(this,{
            oAdapter.urls=it
            oAdapter.notifyDataSetChanged()
        })
        binding.rvOrderState.apply {
            layoutManager= LinearLayoutManager(context, LinearLayoutManager.VERTICAL,true)
            adapter=sAdapter
        }
        viewModel.orderStateFlowEvent.observe(this,{
            sAdapter.sl.clear()
            sAdapter.sl.addAll(it)
            sAdapter.notifyDataSetChanged()
        })
        viewModel.onRefreshFinishEvent.observe(this,{
            binding.smartCommon.finishRefresh(it)
            if (!it){
                showErrorStatePage()
            }else{
                showSuccessStatePage()
            }
        })
        binding.tvYezhuPhone.setOnClickListener {

            DialogHelper.showBottomListDialog(
                requireContext(),
                "联系电话",
                arrayOf(viewModel.reportPhone.get().toString())
            ) { _: Int, text: String? -> text?.let { it1 -> callPhone(it1) } }
        }
        binding.tvWuyePhone.setOnClickListener{

            DialogHelper.showBottomListDialog(
                requireContext(),
                "联系电话",
                arrayOf(viewModel.orderMaintainPhone.get().toString())
            ) { _: Int, text: String? -> text?.let { it1 -> callPhone(it1) } }
        }
        LiveBusCenter.observeRefreshComplaintEvent(this,{
            when (it.msg){
                ComplaintType.TAKE,ComplaintType.HANDLE->viewModel.getData()
                ComplaintType.TURN->back()
//                OrderType.CANCEL_ORDER->back()
            }
        })
        LiveBusCenter.observeScoreCompleteEvent(this,{
            viewModel.getData()
        })
    }

    private fun callPhone(phoneNum: String) {
        val intent = Intent(Intent.ACTION_DIAL)
        val data: Uri = Uri.parse("tel:$phoneNum")
        intent.data = data
        startActivity(intent)
    }

    override fun reload() {
        super.reload()
        binding.smartCommon.autoRefresh()
    }
}